package web.util;

import cn.zbmao.fc.FcTool;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.highlight.*;
import org.apache.lucene.search.highlight.Formatter;
import org.apache.lucene.util.Version;

import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2014/12/24.
 */

/**
 * 字符串过滤 工具类
 * */
public class StringOperateUtil {
    private static Logger log = Logger.getLogger("StringOperateUtil.class");

    //不区分大小写
    public static String exChange(String str){
        if(str!=null) {
            return str.toLowerCase();
        }else{
            return "";
        }

    }

    //将html中元素名转为空格
    public static String reA(String str){
        str=str.replaceAll("<a[^>]*href=[\"'](?<url>[^\"']*?)[\"'][^>]*>"," ");
        return str;
    }

//    过滤特殊字符，转换为空格，并将连续空格转为单个空格
    public static String reString(String str){
        str=str.replaceAll("[\\pP \\pS ]", " ").replaceAll("\\s+"," ");
        return str.trim();

    }

    //    过滤特殊字符，转换为""
    public static String reStrings(String str){
        str=str.replaceAll("[\\pP \\pS ]", "");
        return str.trim();

    }

    //移除引号
    public static String reStringsYh(String str){
        str=str.replaceAll("['|\"|‘|’|“|”]", "");
        return str.trim();

    }

    //    过滤特殊字符，转换为""
    public static String reJson(String str){
        str=str.replaceAll("[\\[\"\\] ]", "");
        return str.trim();

    }


//    通过
    public static String updateStrings(String str){
        Pattern p_String=Pattern.compile("([0-9|A-z|\u4e00-\u9fa5])+");
        Matcher m_String = p_String.matcher(str);
        String returnStr="";
        while (m_String.find()){
            returnStr+=m_String.group()+" ";
        }
        return returnStr.trim();
    }

    /**
     * 操作用户输入
     * @param keyword
     * @return
     */
    public static String returnKey(String keyword) {
        if (keyword.equals("+")) {
            return "";
        } else {

            //去除前后各种空格
            keyword = StringUtils.trim(keyword);
            //操作空格
            if (keyword.indexOf(" ") != -1) {
                String str = "";
                String[] keys = keyword.split(" ");
                for (int i = 0; i < keys.length; i++) {
                    //获得每一个整字
                    String key = keys[i];
                    if (key.length() > 2) {
                        //获得分开的分词
//                    key=FcUtil.search_fc(key);
                        key = FcTool.getFc().indexFc(key);
                        //除去分词后面的最后一个空格
                        key = key.substring(0, key.length() - 1);
                        //给分开的分词加上双引号。
                        key = "\"" + key + "\" ";
                        //获得带引号的key值
                    } else {
                        key = "\"" + key + "\" ";
                    }
                    str += key;
                }
                return str.substring(0, str.length() - 1);
            }
            //操作加号用户输入
            if (keyword.indexOf("+") != -1) {
                String str = "+";
                String[] keys = keyword.split("[+]");
                for (int i = 0; i < keys.length; i++) {
                    //获得每一个整字
                    String key = keys[i];
                    if (key.length() > 2) {
                        //获得分开的分词
//                    key=FcUtil.search_fc(key);
                        key = FcTool.getFc().indexFc(key);
                        //除去分词后面的最后一个空格
                        key = key.substring(0, key.length() - 1);
                        //给分开的分词加上双引号。
                        key = "\"" + key + "\" +";
                        //获得带引号的key值
                    } else {
                        key = "\"" + key + "\" +";
                    }
                    str += key;
                }
                return str.substring(0, str.length() - 2);
            }
            //没有任何符号操作的用户输入
            String str = "+";
            String key = "";
            if (keyword.length() > 2) {
//            key=FcUtil.search_fc(keyword);
                key = FcTool.getFc().indexFc(keyword);

                key = key.substring(0, key.length());
                key = "\"" + key + "\"";

                str = key;
            } else {
                str = keyword;
            }
            return str;
        }
    }
    /**
     * 获得关键词
     * @param keyword
     * @return
     */
    public static List<String> getKeyword(String keyword){
        List<String> strs=new ArrayList<String>();
        if(keyword.indexOf("+")!=-1){
            String [] key=keyword.split("[+]");
            for (int i = 0; i < key.length; i++) {
                strs.add(key[i]);
            }
            return strs;
        }
        if(keyword.indexOf(" ")!=-1){
            String [] key=keyword.split(" ");
            for (int i = 0; i < key.length; i++) {
                strs.add(key[i]);
            }
            return strs;
        }
        strs.add(keyword);
        return strs;
    }

    public static String getUnKeyWord(String keyword){
        String unKeyWord="";
        String []str=FcTool.getFc().indexFc(keyword.trim()).trim().split(" ");
        for (String key : str) {
            unKeyWord+=" -"+key;
        }
        if(unKeyWord.trim().equals("-")){
            return "";
        }
        return unKeyWord;
    }


    //将list转换为map形式
    public static Map<String,Object> getGraphicStr(List<Map<String,Object>> list ,String name){
        Map<String,Object> map1=new HashMap<String, Object>();
        String str="";
        for(Map<String,Object> map:list){
            str+=map.get(name)+" ";
        }
        map1.put(name,str);
    return map1;

    }

    public static String getFcKey(String subscriber){
        StringBuffer sbf=new StringBuffer();
        String ss= StringOperateUtil.reString(subscriber);
        String[] str=ss.split(" ");
        for(String sea:str) {
            String sp = FcTool.getFc().indexFc(sea);
            String[] s2=sp.split(" ");
            for(int i=0;i<s2.length;i++){
                if(i==0 && i==s2.length-1){
                    sbf.append(" (+"+s2[i]+") ");
                }else if(i==0){
                    sbf.append(" (+"+s2[i]);
                }
                else if(i==s2.length-1){
                    sbf.append(" +"+s2[i]+") ");
                }else{
                    sbf.append(" +"+s2[i]);
                }
            }
        }
        return  sbf.toString();
    }
    public static String getFcKeyWord(String str){
        String [] s=str.trim().split(" ");
        if(s.length>1){
            StringBuffer sbf=new StringBuffer();
            for(int i=0;i<s.length;i++){
                String []st =FcTool.getFc().indexFc(s[i]).split(" ");

                    sbf.append("+(");

                for(int j=0; j<st.length;j++){
                    sbf.append(" +"+st[j]);
                }
                sbf.append(")");
            }
            return sbf.toString();
        }else{
            return FcTool.getFc().indexFc(str);
        }
    }
    public static String getFcKeyW(String str){
        String [] s=str.trim().split(" ");
        if(s.length>1){
            StringBuffer sbf=new StringBuffer();
            for(int i=0;i<s.length;i++){
                String []st =FcTool.getFc().indexFc(s[i]).split(" ");

                sbf.append("(");

                for(int j=0; j<st.length;j++){
                    sbf.append(" +"+st[j]);
                }
                sbf.append(")");
            }
            return sbf.toString();
        }else{
            return FcTool.getFc().indexFc(str);
        }
    }
    public static String getQualificationFc(String[]qualification ){
        StringBuffer sbf=new StringBuffer();
        for(int a=0;a<qualification.length;a++){
            if(a==0){

                if(qualification.length==1){
                    sbf.append(" +("+qualification[a]+")");
                }else{
                    sbf.append(" +("+qualification[a]);
                }
            }else if(a==qualification.length-1){
                sbf.append(" "+qualification[a]+")");
            }else{
                sbf.append(" "+qualification[a]);
            }
        }
        return sbf.toString();
    }

   public static List<Map<String,Object>> selectSort(List<Map<String,Object>>list) {
        List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();
        int size = list.size();
        int temp = 1;
        if (size > 1) {
            for (int i = 0; i < size; i++) {
                temp = 1;
                for (int j = 0; j < size; j++) {
                    int num1 = Integer.parseInt(list.get(j).get("num") + "");
                    int num2 = Integer.parseInt(list.get(temp).get("num") + "");

                    if (num1 == -1 && num2 == -1) {
                    } else {
                        if (num1 > num2) {
                            temp = j;
                        }
                    }
                }
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("subscriber", list.get(temp).get("subscriber"));
                map.put("num", list.get(temp).get("num"));
                list1.add(map);
                list.get(temp).put("num", -1);
            }
        } else {
            list1.addAll(list);
        }
        return list1;
   }
    public static List<Map<String,Object>> selectSort1(List<Map<String,Object>>list) {
        List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();
        int size = list.size();
        int temp = 1;
        if (size > 1) {
            for (int i = 0; i < size; i++) {
                temp = 1;
                for (int j = 0; j < size; j++) {
                    Double num1 = Double.parseDouble(list.get(j).get("precision") + "");
                    Double num2 = Double.parseDouble(list.get(temp).get("precision") + "");

                    if (num1 == -1 && num2 == -1) {
                    } else {
                        if (num1 > num2) {
                            temp = j;
                        }
                    }
                }
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("win_bid_company", list.get(temp).get("win_bid_company"));
                map.put("precision", list.get(temp).get("precision"));
                list1.add(map);
                list.get(temp).put("precision", -1);
            }
        } else {
            list1.addAll(list);
        }
        return list1;
    }
    public static List<Map<String,Object>> selectSort2(List<Map<String,Object>> list){
        List<Map<String,Object>> list1=new ArrayList<Map<String, Object>>();
        List<String> list2=new ArrayList<String>();
        boolean b=false;
        for(Map<String,Object>map: list ){
            String subscriberContent=map.get("subscriber_contact")+"";
            if(subscriberContent.equals("")){
                b=true;
                continue;
            }
            if(!list2.contains(subscriberContent)){
                list2.add(subscriberContent);
            }
        }
        if(b){
            list2.add("");
        }
        for(String str:list2){
            for(Map<String,Object> map1 :list){
                if(map1.get("subscriber_contact").equals(str)){
                    list1.add(map1);
                }
            }
        }
        return list1;
    }

    public static String getItemAmount(int num){
        if(num==0){
            return "不限";
        }else if(num==1){
            return "20万以下";
        }else if(num==2){
            return "20万到50万";
        }else if(num==3){
            return "50万到100万";
        }else if(num==4){
            return "100万到300万";
        }else if(num==5){
            return "300万到600万";
        }else if(num==6){
            return "600万到1000万";
        }else if(num==7){
            return "1000万以上";
        }
        return "";
    }

//    //关键词比较
//    public static String updateKeyWord(String key){
//        List<String> str=new ArrayList<String>();
//        List<String>list=Arrays.asList(key.trim().split(" "));
//        for(int i=0;i<list.size();i++){
//            for(int j=0;j<list.size();j++){
//                String stri=list.get(i);
//                String strj=list.get(j);
//                if(stri.contains(strj)){
//                    if(!str.contains(strj)){
//                        str.add(strj);
//                    }
//                    String stt=stri.substring(0,stri.indexOf(strj));
//                    if(!stt.trim().equals("") &&str.indexOf(stt)==-1){
//                        str.add(stt);
//                    }
//                    String st=stri.substring(stri.indexOf(strj)+strj.length());
//                    if(!stt.trim().equals("") &&str.indexOf(stt)==-1){
//                        str.add(st);
//                    }
//                }
//            }
//        }
//        return str.toString().trim();
//    }
    //关键词比较
    public static String updateKeyWord(String key,String content){
//        Map<String,Object>map=new HashMap<String, Object>();
        String str="";
        String str1="";
        List<String>list=Arrays.asList(key.trim().split(" "));

        for(int i=0;i<list.size();i++){
            String stri=list.get(i);
            boolean b=true;
            for(int j=0;j<list.size();j++){
                String strj=list.get(j);
                if(!stri.equals(strj) &&strj.contains(stri)){
                    b=false;

                    if(content.contains(strj)){

                        if(str1.indexOf(stri+" ")==-1) {
                            str1 += stri + " ";
                        }
                    }


                }
            }

        }
        key=" "+key+" ";
        String [] unStr=str1.trim().split(" ");
        for (String s : unStr) {
            if(!s.equals("")) {
                key = key.replaceAll(" "+s + " ", " ");
            }
        }
        return key.trim();
    }

    /**
     * lucene搜索字符串处理
     * @param kewWord
     * @param relation
     * @return
     */
    public static String updateRelation(String kewWord,String relation){
        StringBuffer sbf=new StringBuffer();
        String []strs=reString(kewWord).split(" ");
        for (String str : strs) {
            if(sbf.length()==0){
                sbf.append(str+" ");
            }else{
                sbf.append(relation+" "+str+" ");
            }
        }
        return sbf.toString().trim();
    }
    /**
     * lucene搜索字符串处理
     * @param kewWord
     * @param relation
     * @return
     */
    public static String updateRelation_DY(String kewWord,String relation){
        if(kewWord.equals("") ||kewWord.equals("[]")  ){
            return "";
        }
        StringBuffer sbf=new StringBuffer();
        String []strs=reString(kewWord).split(" ");
        for (String str : strs) {
            if(sbf.length()==0){
                sbf.append("\""+str+"\" ");
//                sbf.append(str+" ");
            }else{
                sbf.append(relation+" \""+str+"\" ");
//                sbf.append(relation+" "+str+" ");
            }
        }
        return sbf.toString().trim();
    }


    /**
     * 将list数据转换为dataTable 可读取数据
     * @param list
     * @return
     */
    public static List<List<String>> updateList(List<Map<String,Object>> list,String key){
        List<List<String>> listNews =new ArrayList<List<String>>();

        int id=1;
        for (Map<String, Object> news : list) {
            if(news.get("title")==null || news.get("title").equals("")){
                continue;
            }
            String url = "http://zb.zbmao.cn/news/snapShot?districtNewsId="+news.get("district_news_id")+"&keyList="+key+"&type=0&location="+news.get("province_id");
            List<String>list2=new ArrayList<String>();
            list2.add(id+"");
            list2.add("<a href='"+url+"' title='"+news.get("title")+"' target='_blank'>"+news.get("title_key")+"");
            if(Integer.parseInt(news.get("etime")+"")==0){
                list2.add("");
            }else {
                list2.add(news.get("endTime") + "");
            }

            double d= Double.parseDouble(news.get("item_amount").toString())/10000;
            if(d==0){
                list2.add("0");
            }else {
                list2.add(DoubleOpeaterUtil.updateDouble("#.0000", d));
            }
            list2.add(news.get("qualification") + "");
            list2.add(news.get("district")+"");
            listNews.add(list2);
            id++;
        }
        return listNews;
    }


    /**
     *
     * @param key
     * @return
     */
    public static String updateList(String [] key){
        String str="";
        for (String s : key) {
            boolean b= true;
            for (String s1:key){
                if(!s.equals(s1) && s1.contains(s) && !s.contains("-")  && !s1.contains("-") ){
                    b=false;
                }
            }
            if(b){
                str=str+" "+s;
            }
        }
        return str.trim();
    }

    /**
     * 提取摘要测试方法
     * @param content
     * @param key
     * @return
     */
    public static String testSummary(String content,String key){
        key=key.replaceAll("-","").replaceAll("\\+","").replaceAll("\\s+", " ").trim();
        int subEnd=content.length()-1;
        String[] keyArray=key.split(" ");
        String rs="";
        for(String k:keyArray){
            if(content.indexOf(k)!=-1){
                int s=content.indexOf(k);
                String str=content.substring(s>40?s-40:0,(s+40)>subEnd?subEnd:(s+40));
                rs+=str+",";
                content=content.replace(str,"");
                subEnd=content.length()-1;
            }
        }
        /* 若摘要为空，返回空 */
        if(rs.length()<=2){
            return "";
        }
        if(rs.length()<80){
            rs+=content.substring(0,(80-rs.length())<subEnd?(80-rs.length()):subEnd)+",";
        }
        rs=rs.substring(0,rs.length()-1);

        rs=luceneSummarySize(rs,key,80);
        rs=HighlightToText.highlight(StringOperateUtil.exChange(rs), keyArray);

        /* 排除部分含有div的字符串 */
        rs=rs.replaceAll("div","").replaceAll("/div","");
        return rs;
    }

    /**
     * 获取摘要
     * @param txt
     * @param pQuery
     * @return
     * @throws java.io.IOException
     * @throws InvalidTokenOffsetsException
     * @throws ParseException
     */
   public static String luceneSummary(String txt,String pQuery) throws IOException, InvalidTokenOffsetsException, ParseException {
       long logTime=System.currentTimeMillis();
       long log_=System.currentTimeMillis();
       String logStr="";
       pQuery=pQuery.replaceAll("-","").replaceAll("\\+","");

//        Formatter formatter = new SimpleHTMLFormatter("<font color='#c00'>","</font>"); //高亮
        Formatter formatter = new SimpleHTMLFormatter("","");
        Analyzer analyzer = new CJKAnalyzer(Version.LUCENE_46);
        QueryParser parser = new QueryParser(Version.LUCENE_46, "", analyzer);
        Highlighter highlighter = new Highlighter(formatter , new QueryScorer(parser.parse(pQuery)));

        Fragmenter fragmenter = new SimpleFragmenter(150);
        highlighter.setTextFragmenter(fragmenter);

//        String rs=highlighter.getBestFragment(analyzer, "", txt);
        List<String> lists = StringOperateUtil.getKeyword(pQuery.trim().replaceAll("-",""));
        int size =lists.size();
        String[] str = new String[size];
        for(int i=0;i<lists.size();i++){
            str[i]=lists.get(i).trim();
        }

//		 Fragmenter fragmenter = new SimpleFragmenter(100);
//		 highlighter.setTextFragmenter(fragmenter);
        highlighter.setTextFragmenter(new NullFragmenter()); //不要限制子多少
        String[] strArray = txt.split("([。|，|,|.])");  //以逗号分隔传入的文章，逐句的提取摘要，这样的摘要最起码是一个句子
        String text = "";
        for(String str1 : strArray){  //循环每句话
            String ret = highlighter.getBestFragment(analyzer, "", str1);
            if(ret!=null){
                text += ret+",";
            }
        }
       text=text.replaceAll("\\n", "").replaceAll("\\s+", "").trim();

       if(text.length()<70){
           String[] t1=text.split(",");
           int i=txt.indexOf(t1[0])+t1.length;
           int i_sub=70-text.length()+i;
           String s1="";
           if(txt.length()>i_sub){
               s1=txt.substring(i,i_sub);
           }
           if(t1.length>=2 && s1.contains(t1[1])){
               text=t1[0]+","+s1;
           }else{
               text=text+","+s1;
           }
       }

       logStr+="正文提取摘要耗时_："+(System.currentTimeMillis()-logTime)+";\n";
       logTime=System.currentTimeMillis();

       text=getKeySummary(text, pQuery, 80);
       logStr+="按关键词调整摘要耗时_："+(System.currentTimeMillis()-logTime)+";\n";
       logTime=System.currentTimeMillis();

       text=luceneSummarySize(text,pQuery,80);
       logStr+="截取长度耗时_："+(System.currentTimeMillis()-logTime)+";\n";
       logTime=System.currentTimeMillis();

       text=HighlightToText.highlight(StringOperateUtil.exChange(text), str);
       logStr+="高亮耗时_："+(System.currentTimeMillis()-logTime)+";\n";
       logStr+="摘要总耗时_："+(System.currentTimeMillis()-log_)+";\n";
       log.info(logStr+"---------------------");
        return text;

       //长度处理
      /* rs=luceneSummarySize(rs,pQuery,80);
       rs=HighlightToText.highlight(StringOperateUtil.exChange(rs), str);
       return rs;*/
   }

    /**
     * 先按每个关键词只提取一条，长度不够在添加其他
     * @param summary
     * @param key
     * @param length
     * @return
     */
    public static String getKeySummary(String summary,String key,int length){
        //摘要中包含了的关键词
        List<String> keyList=new ArrayList<String>();
        for(String s:key.split(" ")){
            if(summary.contains(s)){
                keyList.add(s);
            };
        }
        //按每个关键词只提取一条摘要
        List<String> keyRs=new ArrayList<String>();
        String sumRs="";
        String sumBC="";
        String[] arrayStr=summary.split("[,|.|，|。|、|；|：|:|;|！|!]");
        for(String s2:arrayStr){
            for(String s:keyList){
                if(keyRs.size()<keyList.size()){
                    if(s2.contains(s) && !keyRs.contains(s)){
                        if(!sumRs.contains(s2))sumRs+=s2+" ";
                        keyRs.add(s);
                    }else{
                        if(!sumBC.contains(s2))sumBC+=s2+" ";
                    }
                }else{
                    if(!sumBC.contains(s2))sumBC+=s2+" ";
                }
            }
        }
        //提取后的长度判断
        int num=0;
        String[] sumBcArray=sumBC.split(" ");
        while (get_length(sumRs)<length*2 && sumBcArray.length>num){
            sumRs+=sumBcArray[num]+" ";
            num++;
        }

        sumRs=sumRs.trim().replaceAll("\\s+"," ").replaceAll(" ", "，");
//        if(sumRs.length()>0)sumRs=sumRs.substring(0,sumRs.length()-1);
        return sumRs;
    }


    /**
     * 摘要长度截取
     * @param summary
     * @param key
     * @param length
     * @return
     */
    public static   String luceneSummarySize(String summary,String key,int length){
        if(summary==null || summary.length()<=length){
           return summary;
        }
        summary=reA(summary).replaceAll("\\n", "").replaceAll("\\s+","").replaceAll(" ","").trim();
        if(summary.length()<=length){
            return summary;
        }

        //System.out.println(summary);
        int s_length=get_length(summary);//长度
        int sub=s_length-length*2;//删除个数
        if(sub<=0){
            return summary;
        }

        /* 分段删除 */
        String[] arrayStr=summary.split("[,|.|，|。|、|；|：|:|;|！|!]");
        for(int i=0;i<arrayStr.length;i++){
            String str=arrayStr[i];
            //包含关键词的
            if(get_Index(str,key)!=-1){
                String s_0=delete_str(str,get_Index(str,key),sub);
                arrayStr[i]=s_0;
                sub-=(get_length(str)-get_length(s_0));
                if(sub<=0)break;

            //不包含关键词的，直接删除
            }else{
                String s_0=delete_str(str,str.length(),sub);
                arrayStr[i]=s_0;
                sub-=(get_length(str)-get_length(s_0));
                if(sub<=0)break;
            }
        }

        String arr_Str="";
        for(String str:arrayStr){
            arr_Str+=(str)+" ";
        }
        //arr_Str=arr_Str.substring(1,arr_Str.length());

        if(sub>0){
            arr_Str=agentSize(arrayStr,key,sub);
            //System.out.println("二次处理后长度："+get_length(arr_Str)+",sub:"+sub);
        }



//        System.out.println("原始长度：" + get_length(summary) + ",处理后长度：" + get_length(arr_Str));
//        System.out.println("处理后："+arr_Str);
//        System.out.println("======================================================");

        //返回数据
        arr_Str=arr_Str.trim().replaceAll("\\s+"," ").replaceAll(" ","，");
        return arr_Str;
    }
    public static boolean isChinese(char c) {
        boolean result = false;
        if (c >= 19968 && c <= 171941) {// 汉字范围 \u4e00-\u9fa5 (中文)
            result = true;
        }
        return result;
    }
    public static int get_length(String s){
        int num=0;
        for(char c:s.toCharArray()){
           if(isChinese(c)){
               num+=2;
           }else{
               num+=1;
           }
        }
        return num;
    }

    //判断是否包含关键词
    public static int get_Index(String content,String key){
        List<String> list=getKeyword(key);
        int index=-1;
        int num=-1;

        for(String str:list){
            if(content.indexOf(str)>=0 /*&& content.indexOf(str)>index*/){
                index=content.indexOf(str);
                if(num==-1)num=index;
                if(num>index)num=index;
            }
        }
        return  num;
    }
    public static int get_Index_0(String content,String key){
        List<String> list=getKeyword(key);
        int index=-1;
        for(String str:list){
            if(content.indexOf(str)>=0 && content.indexOf(str)>index)
                index=content.indexOf(str)+str.length();
        }
        return  index;
    }
    public static String  delete_str(String str,int index,int sub){
        char [] array=str.toCharArray();
        for(int i=0;i<index;i++){
            if(isChinese(array[i])){
                sub-=2;
            }else{
                sub-=1;
            }
            array[i]=' ';
            if(sub<=0)break;
        }

        //返回
        String rs="";
        for(char a:array){
            rs+=a;
        }
        rs=rs.replaceAll("\\s+","");
        str=rs;
        return rs;
    }
    public static String  delete_str_0(String str,int index,int sub){
        char [] array=str.toCharArray();
        for(int i=array.length-1;i>index;i--){
            if(isChinese(array[i])){
                sub-=2;
            }else{
                sub-=1;
            }
            array[i]=' ';
            if(sub<=0)break;
        }

        //返回
        String rs="";
        for(char a:array){
            rs+=a;
        }
        rs=rs.replaceAll("\\s+","");
        str=rs;
        return rs;
    }

    public static String  agentSize(String[] arrayStr,String key,int sub){
        for(int i=arrayStr.length-1;i>=0;i--){
            String str=arrayStr[i];
            //包含关键词的
            if(get_Index(str,key)!=-1){
                String s_0=delete_str_0(str, get_Index_0(str, key), sub);
                arrayStr[i]=s_0;
                sub-=(get_length(str)-get_length(s_0));
                if(sub<=0)break;
            }
        }

        String arr_str="";
        for (String s:arrayStr) arr_str+=s+" ";

        //最后判断
        if(arr_str.length()>80 && sub>0){
            int se1=(arr_str.length()-80)/2;
            String s1=arr_str.substring(0,arr_str.length()/2-se1);
            String e1=arr_str.substring(arr_str.length()/2+se1,arr_str.length());
            arr_str=s1+e1;
        }

        return arr_str;
    }


    public static void main(String[]args){
        /*String str="　　安全1　%& $@@　FG$HGF　 　　安全2　　　 j记得发 啊　　安全3　　　";
        System.out.println(returnKey(str));*/

        /*String str1="黔江区公安局视频会议系统升级及新增设备采购编号QJZFCG16A0604采购" +
                "计划编号采购目录货物类采购方式公开黔江区公安局视频会议系统," +
                "升级及公安局新增设备采购编号QJZFCG16A0604采购计划编号采购目录货物类采购方式公开招标供应商资格要求详见重庆市黔江区公共资源综合交易"+
                "招标供应商资格要求详见重庆市黔江区公共资源综合交易";
        String rs=luceneSummarySize(str1,"公安局",80);*/

        //System.out.println("tset:"+ip2int("171.113.117.207"));
        String ss="\"\"dsd''\"as‘das”d“sa’d\"\"";
        System.out.println(ss+" : "+reStringsYh(ss));

    }

    public static String[] toStringArray(List<String> strList) {
        String[] array = new String[strList.size()];
        strList.toArray(array);
        return array;
    }
//    还原分词
    public static String recoveryFc(String str){
        String[] strs = str.split(" ");
        String newStr = strs[0];
        for(int i=1;i<strs.length;i++){
            if(isChineseByREG(strs[i]) &&  strs[i].length()==2) {
                newStr += strs[i].substring(1);
            }else{
                newStr += strs[i];

            }
        }
        return newStr;
    }

//    判读汉字
    public static boolean isChineseByREG(String str) {
        if (str == null) {
            return false;
        }
        Pattern pattern = Pattern.compile("[\\u4E00-\\u9FBF]+");
        return pattern.matcher(str.trim()).find();
    }

}
